package com.begye.smartloan.apptest.huankuan

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.AtPinHuanKuanFailBinding
import com.begye.smartloan.apptest.fangkuanzhong.FangKuanZhongActivity
import com.begye.smartloan.apptest.gongong.YongHuLiuChengViewModel
import com.begye.smartloan.apptest.homeadsource.HomeAdSourceActivity
import com.begye.smartloan.apptest.jujue.JuJueActivity
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.renzheng.RenZhengActivity
import com.begye.smartloan.apptest.renzheng.bangka.RenZhengBangKaActivity
import com.begye.smartloan.apptest.renzheng.document.RenZhengDocumentActivity
import com.begye.smartloan.apptest.renzheng.lianxiren.RenZhengLianXiRenActivity
import com.begye.smartloan.apptest.renzheng.selfie.RenZhengSelfieActivity
import com.begye.smartloan.apptest.shenhezhong.ShenHeZhongActivity
import com.begye.smartloan.apptest.tool.PageTagTool
import com.begye.smartloan.apptest.tool.addTouch
import com.util.utillib.extend.IntentFlagMode
import com.util.utillib.extend.copyLabel
import com.util.utillib.extend.openSystemBrowser
import com.util.utillib.extend.toNextActivity

@SuppressLint("CustomSplashScreen")
class PinHuanKuanFailedActivity : AppCompatActivity() {

    private lateinit var _binding: AtPinHuanKuanFailBinding

    private val _huanKuanViewModel by lazy {
        ViewModelProvider(this)[HuanKuanViewModel::class.java]
    }

    private val _yongHuLiuChengViewModel by lazy {
        ViewModelProvider(this)[YongHuLiuChengViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtPinHuanKuanFailBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configYongHuLiuChengState()
        configUIState()
        configUIEvent()
        configUIData()
    }

    private fun configYongHuLiuChengState() {

        _huanKuanViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuLiuChengViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuLiuChengViewModel.onHasOrderStatusLiveData.observe(this) {
            it?.let { result ->
                when (result) {
                    PageTagTool.dai_huan_kuan -> {
                        toNextActivity(
                            activity = HuanKuanActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.fang_kuan_zhong -> {
                        toNextActivity(
                            activity = FangKuanZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.shen_he_zhone -> {
                        toNextActivity(
                            activity = ShenHeZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.ju_jue -> {
                        toNextActivity(
                            activity = JuJueActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }
                }
            }
        }

        _yongHuLiuChengViewModel.onDoingRenZhengPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingLianXiRenPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengLianXiRenActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingBangKaFrPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengBangKaActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingDocumentPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengDocumentActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingSelfiePageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengSelfieActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onAllDoneLiveData.observe(this) {
            if (it.isNotBlank()) {
                // to shen qing ye
                toNextActivity(
                    activity = HomeAdSourceActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun configUIState() {

        _huanKuanViewModel.onPinStatusLiveData.observe(this) {

        }

        _huanKuanViewModel.onHuanKuanUrlLiveData.observe(this) {
            it?.let { result ->
                val isPin = result.optBoolean("isPin", false)
                if (isPin) {
                    val params = HashMap<String, String>().apply {
                        put("orderId", _huanKuanViewModel.pinOrderId)
                        put("thirdOrderId", _huanKuanViewModel.pinThirdOrderId)
                    }
                    toNextActivity(
                        activity = PinHuanKuanProcessingActivity::class.java,
                        mode = IntentFlagMode.SINGLE_TOP,
                        params = params
                    )
                } else {
                    val targetUrl = result.optString("targetUrl", "")
                    if (targetUrl.isNotBlank()) {
                        openSystemBrowser(targetUrl)
                    }
                }
            }
        }
    }

    private fun configUIEvent() {

        _binding.atPinHuanKuanFailNar.initConfig(
            text = getString(R.string.huan_kuan_text_35),
            textColor = "#000000",
            leftResId = R.drawable.dres_39,
            rightResId = R.drawable.dres_38
        ) {
            if (0 == it) {
                toNextActivity(
                    activity = HuanKuanActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        _binding.atPinHuanKuanFailMakeAgain.setOnClickListener {
            _binding.atPinHuanKuanFailMakeAgain.isEnabled = false
            _huanKuanViewModel.requestHuanKuanUrl()

            //
            addTouch(type = "pin_huan_kuan_fail_try_again_click")
        }

        _binding.atPinHuanKuanFailFlowIdCopy.setOnClickListener {
            val text = _binding.atPinHuanKuanFailFlowIdValue.text.toString().trim()
            if (text.isNotBlank()) {
                copyLabel(text, "Success")
            }

            //
            addTouch(type = "pin_huan_kuan_fail_copy_id_click")
        }
    }

    private fun configUIData() {
        // 查询状态
        val product = intent.getStringExtra("product") ?: ""
        val jinE = intent.getStringExtra("jinE") ?: ""
        val pid = intent.getStringExtra("pid") ?: ""
        val failedTime = intent.getStringExtra("failedTime") ?: ""
        val failReason = intent.getStringExtra("failReason") ?: ""

        _binding.atPinHuanKuanFailJinEValue.text = jinE
        _binding.atPinHuanKuanFailProductValue.text = product
        _binding.atPinHuanKuanFailFlowIdValue.text = pid
        _binding.atPinHuanKuanFailTime.text = failedTime
        if (failReason.isNotBlank()) {
            _binding.atPinHuanKuanFailTopTips.text = failReason
            _binding.atPinHuanKuanFailTopTips.visibility = View.VISIBLE

            _binding.atPinHuanKuanFailReasonValue.text = failReason
            _binding.atPinHuanKuanFailReasonValue.visibility = View.VISIBLE

        } else {
            _binding.atPinHuanKuanFailTopTips.visibility = View.GONE
            _binding.atPinHuanKuanFailReasonValue.visibility = View.GONE
        }
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}